﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WMS.BLL;
using WMS.Entity;

namespace WMS.App
{
    public partial class frmGoodsInfo : Form
    {
        #region Private
        private frmCategory fCategory = null;
        private frmUnit fUnit = null;
        private frmIntoOutGoodsLog fRecords = null;
        private int CategoryID { get; set; }
        private Goods goods = null;
        private Category category = null;
        private GoodsLog goodsLog = null;
        private string title = "商品信息 - ";
        #endregion

        private GoodsEntity GoodsInfo { get; set; }

        public frmGoodsInfo()
        {
            InitializeComponent();

            InitData();
        }

        #region Private Methods
        void InitData()
        {
            fCategory = new frmCategory();
            fCategory.ResultCallBackEvent += SetGoodCategory;

            fUnit = new frmUnit();
            fUnit.ResultCallBackEvent += LoadUnitData;

            fRecords = new frmIntoOutGoodsLog();

            goods = new Goods();
            category = new Category();
            goodsLog = new GoodsLog();

            LoadUnitData(null, null);

            if (GoodsInfo == null)
            {
                GoodsInfo = new GoodsEntity();
            }
            this.Text = title + "增加";

            //FillDataToForm(goods.GetGoodsEntity(2));
        }

        void SetGoodCategory(object sender, ResultEventArgs e)
        {
            this.txtGoodCategory.Text = e.Result;
            CategoryID = e.ResultCode;
        }

        void LoadUnitData(object sender, ResultEventArgs e)
        {
            UnitEntity entity = new UnitEntity();
            entity.UnitID = 0;
            entity.UnitName = "";

            Unit unit = new Unit();
            IList<UnitEntity> allUnit = unit.GetAllUnit();
            allUnit.Insert(0, entity);

            this.cmbUnit.DataSource = allUnit;
            this.cmbUnit.DisplayMember = "UnitName";
            this.cmbUnit.ValueMember = "UnitID";
        }

        void FillGoodsInfo()
        {
            GoodsInfo.Code = this.txtCode.Text;
            GoodsInfo.Name = this.txtName.Text;
            GoodsInfo.CategoryId = CategoryID;
            GoodsInfo.Specification = this.txtSpecification.Text;
            GoodsInfo.Unit = int.Parse(this.cmbUnit.SelectedValue.ToString());
            GoodsInfo.Barcode = this.txtBarCode.Text;
            GoodsInfo.Address = this.txtAddress.Text;
            GoodsInfo.Description = this.txtDescription.Text;
            if (GoodsInfo.GoodsId <= 0)
            {
                GoodsInfo.CreateDate = DateTime.Now;
            }
            GoodsInfo.UpdateDate = DateTime.Now;
        }

        bool ValidateGoodsInfo()
        {
            bool flag = true;
            if (string.IsNullOrEmpty(this.txtName.Text))
            {
                flag = false;
                MessageBoxCommon.Warnings("请填写商品名称！");
                this.txtName.Focus();
            }
            else if (string.IsNullOrEmpty(this.txtGoodCategory.Text))
            {
                flag = false;
                MessageBoxCommon.Warnings("请选择商品类别！");
                this.txtGoodCategory.Focus();
            }
            else if (this.cmbUnit.SelectedIndex <= 0)
            {
                flag = false;
                MessageBoxCommon.Warnings("请选择商品单位！");
                this.cmbUnit.Focus();
            }
            return flag;
        }
        #endregion

        public void FillDataToForm(int goodsID)
        {
            GoodsInfo = goods.GetGoodsEntity(goodsID);
            if (GoodsInfo == null)
            {
                GoodsInfo = new GoodsEntity();
                return;
            }
            this.txtName.Text = GoodsInfo.Name;
            CategoryEntity cate = category.GetCategory(GoodsInfo.CategoryId);
            if (cate != null)
            {
                this.txtGoodCategory.Text = cate.Name;
                CategoryID = GoodsInfo.CategoryId;
            }
            this.cmbUnit.SelectedValue = GoodsInfo.Unit;
            this.txtSpecification.Text = GoodsInfo.Specification;
            this.txtCode.Text = GoodsInfo.Code;
            this.txtBarCode.Text = GoodsInfo.Barcode;
            this.txtAddress.Text = GoodsInfo.Address;
            this.txtDescription.Text = GoodsInfo.Description;

            this.Text = title + "修改";

            this.dataGridView1.DataSource = goodsLog.GetGoodsLogRecords(goodsID);
            
        }
        public void ClearFormValue()
        {
            this.txtName.Text = "";
            this.txtCode.Text = "";
            this.txtGoodCategory.Text = "";
            CategoryID = 0;
            this.cmbUnit.SelectedIndex = 0;
            this.txtSpecification.Text = "";
            this.txtBarCode.Text = "";
            this.txtBarCode.Text = "";
            this.txtAddress.Text = "";
            this.txtDescription.Text = "";
            this.dataGridView1.DataSource = new List<GoodsLogEntity>();
        }

        #region Button Event
        private void btnAddUnit_Click(object sender, EventArgs e)
        {
            fUnit.ShowDialog();
        }

        private void btnAddCate_Click(object sender, EventArgs e)
        {
            fCategory.ShowDialog();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!ValidateGoodsInfo())
                {
                    return;
                }

                FillGoodsInfo();

                if (GoodsInfo.GoodsId > 0)
                {//modify
                    goods.Update(GoodsInfo);
                }
                else
                {//add
                    goods.Add(GoodsInfo);
                }
                MessageBoxCommon.Success();
            }
            catch (Exception ex)
            {
                ErrorInfo.Instance.Add(this.Text, ex.ToString());
                MessageBoxCommon.Fail();
            }
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            if (GoodsInfo.GoodsId > 0)
            {
                FillDataToForm(GoodsInfo.GoodsId);
            }
            else
            {
                ClearFormValue();
            }
        }

        private void frmGoodsInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        private void btnIntoQuantity_Click(object sender, EventArgs e)
        {
            if (GoodsInfo.GoodsId > 0)
            {
                fRecords.SetGoodsLog(GoodsInfo.Name, GoodsInfo.GoodsId.ToString(), 0);
                fRecords.ShowDialog();
            }
        }
        #endregion

        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            foreach (DataGridViewRow row in this.dataGridView1.Rows)
                row.Cells["TypeCN"].Value = row.Cells["Type"].Value.ToString() == "0" ? "入库" : "出库";
        }
    }
}
